package com.dadi.broadcastdemo1;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.core.app.NotificationCompat;

/**
 *  This is implementation of the an earlier example, but new uses a receiver instead of a activity
 *  so a activity -> alarm manager -> receiver -> notification -> activity
 */
public class MyReceiver2 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(Test2Activity.TAG, "received an intent");
        if(intent.getAction().equals(Test2Activity.ACTION)){ // my custom intent
            // --- get the notification ID for the notification;
            // passed in by the Test2Activity
            int notifID = intent.getExtras().getInt("NotifID");
            NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            // --- PendingIntent to launch activity if the user selects
            // the notification ---
            Intent notificationIntent = new Intent(context, Test2Activity.class);
            notificationIntent.putExtra("mytype", "1 minutes later?");
            PendingIntent contentIntent = PendingIntent.getActivity(context, notifID, notificationIntent, 0);
            // create the notification
            Notification notif = new NotificationCompat.Builder(context, Test2Activity.id)
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setWhen(System.currentTimeMillis()). // when the event occurred, now, since noti are stored by time
                    setContentTitle("Time's up!")  // Title message top now
                    .setContentText("This is your alert, currently of the AlarmManager") // message when looking at the notification, second roe
                    .setContentIntent(contentIntent)
                    .setAutoCancel(false) // allow auto cancel when pressed
                    .build(); // finally build and return a Nofitication.
            nm.notify(notifID, notif);
        }
    }
}
